-- ----------------------------------------------------------------------------------------
-- Vistas para poder parametrizar los libros contables con una vista de comprobantes 
-- La vista debe tener codigo 9-LBC y solo los comprobantes de la vista apareceran en los libros
-- contables. Si la vista de comprobantes 9-LBC no existe el sistema se comporta como antes
-- ----------------------------------------------------------------------------------------


CREATE OR REPLACE VIEW vcommov_lbc AS 
 SELECT m.idcomm, m.id, m.tipomov, m.signo, m.piva, m.idctb, m.orden, m.impo, 
    m.fechacomm, m.idctbcos1, m.idctbcos2, m.tipocom, m.codambsuc, m.codctbsub, 
    m.fecharesumen, m.alicuota, m.presupuesto, m.idambdb, m.fechainsert, 
    m.fechaupdate, m.idctbcos3, m.idctbcos4
   FROM commov m
   JOIN comm c ON m.idcomm = c.id
  WHERE NOT (EXISTS ( SELECT comvis.cod, comvis.nombre, comvis.idambdb, 
       comvis.fechainsert, comvis.fechaupdate
      FROM comvis
     WHERE comvis.cod::text = '9-LBC'::text)) OR (c.tipocom::text IN ( SELECT comvisxcom.tipocom
      FROM comvisxcom
     WHERE comvisxcom.codcomvis::text = '9-LBC'::text));

ALTER TABLE vcommov_lbc
  OWNER TO abacouser;

--------------------------

CREATE OR REPLACE VIEW vdiario AS 
         SELECT comm.tipocom, comm.codctbsub, comm.fecha, comm.id, comm.letra, 
            comm.detalle, comm.nroptovta, comm.nro, comm.nroasi, comm.codambsuc
           FROM comm
          WHERE comm.codctbsub::text = 'DI'::text AND (NOT (EXISTS ( SELECT comvis.nombre
                   FROM comvis
                  WHERE comvis.cod::text = '9-LBC'::text)) OR (comm.tipocom::text IN ( SELECT comvisxcom.tipocom
                   FROM comvisxcom
                  WHERE comvisxcom.codcomvis::text = '9-LBC'::text)))
UNION 
         SELECT 'RES'::character varying AS tipocom, comm.codctbsub, 
            comm.fecharesumen AS fecha, 0 AS id, ''::character varying AS letra, 
            'Resumen'::character varying AS detalle, 0 AS nroptovta, 0 AS nro, 
            min(comm.nroasi) AS nroasi, comm.codambsuc
           FROM comm
          WHERE comm.codctbsub::text <> 'DI'::text AND comm.codctbsub::text <> 'SI'::text AND (NOT (EXISTS ( SELECT comvis.nombre
                   FROM comvis
                  WHERE comvis.cod::text = '9-LBC'::text)) OR (comm.tipocom::text IN ( SELECT comvisxcom.tipocom
                   FROM comvisxcom
                  WHERE comvisxcom.codcomvis::text = '9-LBC'::text)))
          GROUP BY comm.fecharesumen, comm.codctbsub, comm.codambsuc;

ALTER TABLE vdiario
  OWNER TO abacouser;



--==========================
SELECT aaVersion('417.00');
--==========================